package utils;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

public class SuDoUtil {

    /**
     * 1 2 3 4 5 6 7 8 9
     * 9 8 7 6 4 5 3 2 1
     * 左上角坐标为 0,0 右下角坐标：8,8
     * 宫的编号，左上为1，右下为9
     */

    static Map<Integer,List<Integer>> gong1= new HashMap<>();
    static Map<Integer,List<Integer>> gong2= new HashMap<>();
    static Map<Integer,List<Integer>> gong3= new HashMap<>();
    static Map<Integer,List<Integer>> gong4= new HashMap<>();
    static Map<Integer,List<Integer>> gong5= new HashMap<>();
    static Map<Integer,List<Integer>> gong6= new HashMap<>();
    static Map<Integer,List<Integer>> gong7= new HashMap<>();
    static Map<Integer,List<Integer>> gong8= new HashMap<>();
    static Map<Integer,List<Integer>> gong9= new HashMap<>();

    static Set<Map<Integer, List<Integer>>> gongs = new HashSet<>();

    static {
        gong1.put(0, Arrays.asList(0,1,2));
        gong1.put(1, Arrays.asList(0,1,2));
        gong1.put(2, Arrays.asList(0,1,2));

        gong2.put(0, Arrays.asList(3,4,5));
        gong2.put(1, Arrays.asList(3,4,5));
        gong2.put(2, Arrays.asList(3,4,5));

        gong3.put(0, Arrays.asList(6,7,8));
        gong3.put(1, Arrays.asList(6,7,8));
        gong3.put(2, Arrays.asList(6,7,8));

        gong4.put(3, Arrays.asList(0,1,2));
        gong4.put(4, Arrays.asList(0,1,2));
        gong4.put(5, Arrays.asList(0,1,2));

        gong5.put(3, Arrays.asList(3,4,5));
        gong5.put(4, Arrays.asList(3,4,5));
        gong5.put(5, Arrays.asList(3,4,5));

        gong6.put(3, Arrays.asList(6,7,8));
        gong6.put(4, Arrays.asList(6,7,8));
        gong6.put(5, Arrays.asList(6,7,8));

        gong7.put(6, Arrays.asList(0,1,2));
        gong7.put(7, Arrays.asList(0,1,2));
        gong7.put(8, Arrays.asList(0,1,2));

        gong8.put(6, Arrays.asList(3,4,5));
        gong8.put(7, Arrays.asList(3,4,5));
        gong8.put(8, Arrays.asList(3,4,5));

        gong9.put(6, Arrays.asList(6,7,8));
        gong9.put(7, Arrays.asList(6,7,8));
        gong9.put(8, Arrays.asList(6,7,8));

        gongs.add(gong1);
        gongs.add(gong2);
        gongs.add(gong3);
        gongs.add(gong4);
        gongs.add(gong5);
        gongs.add(gong6);
        gongs.add(gong7);
        gongs.add(gong8);
        gongs.add(gong9);
    }

 /*   public static void main(String[] args) {
        int[][] source = {
                {5,6,8,3,1,2,4,7,9},
                {4,7,3,9,5,8,1,6,2},
                {9,2,1,6,7,4,5,3,8},
                {8,5,6,2,4,7,9,1,3},
                {7,1,9,8,6,3,2,5,4},
                {2,3,4,1,9,5,6,8,7},
                {3,4,7,5,2,6,8,9,1},
                {1,8,5,4,3,9,7,2,6},
                {6,9,2,7,8,1,3,4,5},
        };
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                System.out.print(source[i][j] + "\t");
            }
            System.out.print("\n");
        }
        boolean check = check(source);
        System.out.println(check);
    }
*/
    public static void main(String[] args) {
        int[][] source = {
                {4,0,0,9,1,3,0,0,8},
                {9,0,5,0,0,0,3,0,2},
                {6,0,0,2,0,4,1,9,7},
                {0,0,1,0,0,0,0,0,0},
                {2,5,0,8,0,0,4,7,3},
                {3,0,9,7,0,6,8,5,0},
                {1,0,2,4,7,9,0,8,0},
                {5,9,8,0,0,0,0,0,0},
                {0,0,0,0,0,0,2,0,0},
        };
        tianshu(source);
    }

    public static void tianshu(int[][] source){
        /** 读取元数组哪些位置为空 */
        List<Integer[]> emptyPosition = new ArrayList<>();
        Map<Integer, Integer> missNumberMap = new HashMap<>();
        missNumberMap.put(1,9);
        missNumberMap.put(2,9);
        missNumberMap.put(3,9);
        missNumberMap.put(4,9);
        missNumberMap.put(5,9);
        missNumberMap.put(6,9);
        missNumberMap.put(7,9);
        missNumberMap.put(8,9);
        missNumberMap.put(9,9);
        int missNumberLength = 81;
        for (int i = 0; i < source.length; i++) {
            for (int j = 0; j < source[i].length; j++) {
                if (source[i][j] == 0)
                    emptyPosition.add(new Integer[]{i,j});
                else {
                    missNumberMap.put(source[i][j], missNumberMap.get(source[i][j]) - 1);
                    missNumberLength--;
                }
            }
        }
        int[] missNumberArr = new int[missNumberLength];
        AtomicInteger index = new AtomicInteger();
        missNumberMap.keySet().forEach(key -> {
            Integer count = missNumberMap.get(key);
            for (Integer i = 0; i < count; i++) {
                missNumberArr[index.getAndIncrement()] = key;
            }
        });
        for (int i = 0; i < missNumberArr.length; i++) {
            System.out.print(missNumberArr[i]);
        }
//        iteration(source, emptyPosition);
        System.out.println();
        System.out.println(missNumberLength);
        System.out.println(emptyPosition.size());
    }

    public static int[][] iteration (int[][] source, List<Integer[]> emptyPosition){
        int[][] copySource = copyArray(source);
        do{


        } while (check(copySource));
        return copySource;
    }

    /**
     * 复制二维数组
     * @param source
     * @return
     */
    public static int[][] copyArray(int[][] source){
        int[][] copySource = new int[9][9];
        for (int i = 0; i < source.length; i++) {
            copySource[i] = source[i].clone();
        }
        return copySource;
    }

    /**
     * 验证数组正确性
     * @param source
     * @return
     */
    public static boolean check(int[][] source){
        Set tempSet = new HashSet();
        /** 1.验证行 */
        for (int i = 0; i < 9; i++) {
            tempSet.clear();
            for (int j = 0; j < 9; j++) {
                if(source[i][j] != 0)
                    tempSet.add(source[i][j]);
            }
            if(tempSet.size() < 9)
                throw new RuntimeException("数独验证行失败");
        }
        /** 2.验证列 */
        for (int i = 8; i >= 0; i--) {
            tempSet.clear();
            for (int j = 0; j < 9; j++) {
                if(source[i][j] != 0)
                    tempSet.add(source[i][j]);
            }
            if(tempSet.size() < 9)
                throw new RuntimeException("数独验证列失败");
        }
        /** 3.验证宫 */
        gongs.forEach(gong -> {
            tempSet.clear();
            Set<Integer> keySet = gong.keySet();
            keySet.forEach(key -> {
                List<Integer> list = gong.get(key);
                list.forEach(value -> {
                    if(source[key][value] != 0)
                        tempSet.add(source[key][value]);
                });
            });
            if(tempSet.size() < 9)
                throw new RuntimeException("数独验证宫失败");
        });
        return true;
    }


}
